﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using EAF.Utility;
using EAF.Web;
using App.Model;
using App.Utility;
using App.Model.Main;
using App.Utility.ComEntity;

namespace App.Admin.Controllers
{
    //[AllowAnonymous]                                //允许匿名访问 跳过core默认授权System.Web.Mvc.AuthorizeAttribute
    [EnableCors("Limit")]                             //跨域方案名称 可以在StartUp 内查看设置内容  
    [Produces("application/json")]               //返回数据的格式 直接约定为Json 
    [Route("BasePlatform/[controller]/[action]")]     //路由 swagger文档正常工作必须保证  内部方法都要明确get post
    [ModelValidate]
    [AdminApiLog]
    [AdminLoginAuth]
    [AdminActionAuth]
    public partial class ApiController : Controller
    {
        public ApiController()
        {

        }

        #region 登录会话

        /// <summary>
        /// 当前登录用户信息（后台管理）
        /// </summary>
        public AdminLoginSession<SysUser> AdminLoginSession
        {
            get
            {
                return HttpContext.GetLoginSession<AdminLoginSession<SysUser>>(HttpItemKey.LoginSession);
            }
        }

        /// <summary>
        /// 当前登录用户信息（其他客户端）
        /// </summary>
        public AppLoginSession<SysUser> AppLoginSession
        {
            get
            {
                return HttpContext.GetLoginSession<AppLoginSession<SysUser>>(HttpItemKey.LoginSession);
            }
        }

        #endregion

        #region 数据上下文

        /// <summary>
        ///当前项目默认库
        /// </summary>
        public AppMainContext AppMain
        {
            get
            {
                return ServiceLocator.GetHttpContextServer<AppMainContext, AppMainContext>(HttpContext);
            }
        }

        /// <summary>
        /// 日志库
        /// </summary>
        public AppLogContext AppLog
        {
            get
            {
                return ServiceLocator.GetHttpContextServer<AppLogContext, AppLogContext>(HttpContext);
            }
        }


        #endregion

        #region 常用方法

        /// <summary>
        /// 记录当前项目默认库行为日志 如果开启了事务且设置了事务描述则优先使用事务的描述 请注意其他库不在此方法记录
        /// </summary>
        /// <param name="behaviorLog"></param>
        [NonAction]
        public void RecordBehavior(RLogBehavior behaviorLog)
        {
            AppMain.AddBehaviorLog(behaviorLog, HttpContext);
        }

        /// <summary>
        /// 获取指定类型表服务
        /// </summary>
        /// <typeparam name="T">容器注册的表服务类型</typeparam>
        /// <returns></returns>
        [NonAction]
        public T GetTableService<T>()
            where T : class
        {
            return ServiceLocator.GetHttpContextServer<T, T>(HttpContext);
        }

        #endregion


    }
}
